package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.dto.CategoryResponse;
import com.learning.platform.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/categories")
public class CategoryController {
    
    private final CategoryService categoryService;
    
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    
    /**
     * 获取所有顶级分类
     */
    @GetMapping
    public ApiResponse<List<CategoryResponse>> getTopCategories() {
        List<CategoryResponse> categories = categoryService.getTopCategories();
        return ApiResponse.success(categories);
    }
    
    /**
     * 获取分类树
     */
    @GetMapping("/tree")
    public ApiResponse<List<CategoryResponse>> getCategoryTree() {
        List<CategoryResponse> tree = categoryService.getAllCategoriesTree();
        return ApiResponse.success(tree);
    }
    
    /**
     * 获取子分类
     */
    @GetMapping("/{parentId}/children")
    public ApiResponse<List<CategoryResponse>> getSubCategories(@PathVariable Long parentId) {
        List<CategoryResponse> categories = categoryService.getSubCategories(parentId);
        return ApiResponse.success(categories);
    }
    
    /**
     * 获取分类详情
     */
    @GetMapping("/{id}")
    public ApiResponse<CategoryResponse> getCategoryById(@PathVariable Long id) {
        CategoryResponse category = categoryService.getCategoryById(id);
        return ApiResponse.success(category);
    }
}


